import Base64 from '@/lib/base64.min'
import User from './userVO'

/**
 * 基础文章内容对象
 */
let ArticleBasicVO = {
  encode: function (articleBasicVO) {
    if (!articleBasicVO) return null
    // 上传者id
    try {
      if (articleBasicVO.userId) {
        articleBasicVO.userId = Base64.encode(articleBasicVO.userId)
      }
    } catch (e) {
      window.console.log('base64 encode error：（上传者id）')
    }
    // 头部
    try {
      if (articleBasicVO.title) {
        articleBasicVO.title = Base64.encode(articleBasicVO.title)
      }
    } catch (e) {
      window.console.log('base64 encode error：（头部标题）')
    }
    // 国籍
    try {
      if (articleBasicVO.country) {
        articleBasicVO.country = Base64.encode(articleBasicVO.country)
      }
    } catch (e) {
      window.console.log('base64 encode error：（作者国籍）')
    }
    // 作者
    try {
      if (articleBasicVO.author) {
        articleBasicVO.author = Base64.encode(articleBasicVO.author)
      }
    } catch (e) {
      window.console.log('base64 encode error：（文章作者）')
    }
    // 朝代
    try {
      if (articleBasicVO.dynasty) {
        articleBasicVO.dynasty = Base64.encode(articleBasicVO.dynasty)
      }
    } catch (e) {
      window.console.log('base64 encode error：（文章朝代）')
    }
    // 创作时间
    try {
      if (articleBasicVO.createTime) {
        articleBasicVO.createTime = Base64.encode(articleBasicVO.createTime)
      }
    } catch (e) {
      window.console.log('base64 encode error：（创作时间）')
    }
    // book
    try {
      if (articleBasicVO.book) {
        articleBasicVO.book = Base64.encode(articleBasicVO.book)
      }
    } catch (e) {
      window.console.log('base64 encode error：（文章出自书名）')
    }
    // 摘要
    try {
      if (articleBasicVO.articleProfile) {
        articleBasicVO.articleProfile = Base64.encode(articleBasicVO.articleProfile)
      }
    } catch (e) {
      window.console.log('base64 encode error：（文章摘要）')
    }
    // 标签
    try {
      if (articleBasicVO.tags) {
        articleBasicVO.tags = Base64.encode(articleBasicVO.tags)
      }
    } catch (e) {
      window.console.log('base64 encode error：（文章摘要）')
    }
    // 风格
    try {
      if (articleBasicVO.style) {
        articleBasicVO.style = Base64.encode(articleBasicVO.style)
      }
    } catch (e) {
      window.console.log('base64 encode error：（文章摘要）')
    }
    // 解析
    try {
      if (articleBasicVO.analysis) {
        articleBasicVO.analysis = Base64.encode(articleBasicVO.analysis)
      }
    } catch (e) {
      window.console.log('base64 encode error：（文章解析）')
    }
    // 段落
    let sectionList = articleBasicVO.contents
    for (let j in sectionList) {
      let section = sectionList[j]
      try {
        if (section.content) {
          section.content = Base64.encode(section.content)
        }
        if (section.fontColor) {
          section.fontColor = Base64.encode(section.fontColor)
        }
      } catch (e) {
        window.console.log('base64 encode error：（段落文本内容' + parseInt(j) + '段')
      }
    }
    return articleBasicVO
  },

  decode: function (articleBasicVO) {
    if (!articleBasicVO) return null
    // 上传者id
    try {
      if (articleBasicVO.userId) {
        articleBasicVO.userId = Base64.decode(articleBasicVO.userId)
      }
    } catch (e) {
      window.console.log('base64 decode error：（上传者id）')
    }
    // 头部
    try {
      if (articleBasicVO.title) {
        articleBasicVO.title = Base64.decode(articleBasicVO.title)
      }
    } catch (e) {
      window.console.log('base64 decode error：（头部头部标题）')
    }
    // 作者
    try {
      if (articleBasicVO.author) {
        articleBasicVO.author = Base64.decode(articleBasicVO.author)
      }
    } catch (e) {
      window.console.log('base64 decode error：（文章作者）')
    }
    // 国籍
    try {
      if (articleBasicVO.country) {
        articleBasicVO.country = Base64.decode(articleBasicVO.country)
      }
    } catch (e) {
      window.console.log('base64 decode error：（作者国籍）')
    }
    // 朝代
    try {
      if (articleBasicVO.dynasty) {
        articleBasicVO.dynasty = Base64.decode(articleBasicVO.dynasty)
      }
    } catch (e) {
      window.console.log('base64 decode error：（文章朝代）')
    }
    // 创作时间
    try {
      if (articleBasicVO.createTime) {
        articleBasicVO.createTime = Base64.decode(articleBasicVO.createTime)
      }
    } catch (e) {
      window.console.log('base64 decode error：（创作时间）')
    }
    // book
    try {
      if (articleBasicVO.book) {
        articleBasicVO.book = Base64.decode(articleBasicVO.book)
      }
    } catch (e) {
      window.console.log('base64 decode error：（文章出自书名）')
    }
    // 摘要
    try {
      if (articleBasicVO.articleProfile) {
        articleBasicVO.articleProfile = Base64.decode(articleBasicVO.articleProfile)
      }
    } catch (e) {
      window.console.log('base64 decode error：（文章摘要）')
    }
    // 标签
    try {
      if (articleBasicVO.tags) {
        articleBasicVO.tags = Base64.decode(articleBasicVO.tags)
      }
    } catch (e) {
      window.console.log('base64 decode error：（文章摘要）')
    }
    // 风格
    try {
      if (articleBasicVO.style) {
        articleBasicVO.style = Base64.decode(articleBasicVO.style)
      }
    } catch (e) {
      window.console.log('base64 decode error：（文章摘要）')
    }
    // 解析
    try {
      if (articleBasicVO.analysis) {
        articleBasicVO.analysis = Base64.decode(articleBasicVO.analysis)
      }
    } catch (e) {
      window.console.log('base64 decode error：（文章解析）')
    }
    // 段落
    let sectionList = articleBasicVO.contents
    for (let j in sectionList) {
      let section = sectionList[j]
      try {
        if (section.content) {
          section.content = Base64.decode(section.content)
        }
        if (section.fontColor) {
          section.fontColor = Base64.decode(section.fontColor)
        }
      } catch (e) {
        window.console.log('base64 decode error：（段落文本内容' + parseInt(j) + '段')
      }
    }
    return articleBasicVO
  }
}

export default {
  encode: function (articleVO) {
    if (!articleVO) return
    User.encode(articleVO.User)
    ArticleBasicVO.encode(articleVO)
    // ArticleBasicVO.encode(articleVO.DraftItem)
    return articleVO
  },

  decode: function (articleVO) {
    if (!articleVO) return articleVO
    let beginAt = new Date().getTime()
    User.decode(articleVO.User)
    ArticleBasicVO.decode(articleVO)
    // ArticleBasicVO.decode(articleVO.DraftItem)
    let endAt = new Date().getTime()
    window.console.log('[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[')
    window.console.log('articleVO解码时间: ')
    window.console.log('beginAt:  ' + beginAt)
    window.console.log('endAt:    ' + endAt)
    window.console.log('duration: ' + (endAt - beginAt))
    window.console.log(']]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]')
    return articleVO
  }
}
